package com.jic.market.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.market.common.base.MarketBaseService;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.entity.PromotionEntity;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.entity.Tag;
import com.jic.market.mapper.PromotionEntityMapper;
import com.jic.market.mapper.TagMapper;
import com.jic.market.request.PromotionRequest;
import com.jic.market.request.TagRequest;
import com.jic.market.response.PromotionResponse;
import com.jic.market.response.TagResponse;
import com.jic.market.service.TagService;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 活动标签表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-10 10:38:25
 */
@Slf4j
@Service
public class TagServiceImpl extends MarketBaseService implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private PromotionEntityMapper promotionEntityMapper;

    @Override
    public long insert(Tag tag) {
        long flag = tagMapper.insert(tag);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return tagMapper.delete(id);
    }

    @Override
    public long update(Tag tag) {
        return tagMapper.update(tag);
    }

    @Override
    public Tag queryByPrimaryKey(Long id) {
        return tagMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Tag> query(Tag tag) {
        return tagMapper.query(tag);
    }

    @Override
    public RestResult<PageResult<TagResponse>> queryPage(TagRequest param) {
        return executeQuery(() -> {
            int startIndex = (param.getStart() - 1) * param.getLimit();
            TagRequest tag = new TagRequest();
            BeanUtils.copyProperties(param, tag);
            List<TagRequest> tagResponseList = tagMapper.queryPage(startIndex, param.getLimit(), tag);
            long count = tagMapper.queryPageCount(tag);
            PageResult result = new PageResult();
            result.setRows(tagResponseList);
            result.setTotal(count);
            return result;
        });
    }

    @Override
    public RestResult<Integer> addTag(Tag param) {
        return executeQuery(() -> {
            Tag tag = new Tag();
            BeanUtils.copyProperties(param, tag);
            TagRequest request = new TagRequest();
            String tagName = param.getTagName();
            request.setTagName(tagName);
            long total = tagMapper.queryByTagNameCount(tagName);
            if (total != 0) {
                log.error("名称保存异常，tagName={}");
                throw new MarketException(MarketErrorCodeEnum.TAGNAME_ERROR, "此标签名已被使用，请重新命名");
            } else {
                tag.setDeleteFlag(false);
                tag.setCreateTime(new Date());
                tag.setCreatorName(param.getAdminOperate().getOperateName());
                tag.setDeptId(param.getAdminOperate().getDeptId());
                long flag = tagMapper.insert(tag);
            }
            return tag.getId();
        });
    }

    @Override
    public RestResult<PageResult<TagResponse>> getTagList(Tag param) {
        return executeQuery(() -> {
            Tag tag = new Tag();
            BeanUtils.copyProperties(param, tag);
            tag.setDeleteFlag(false);
            List<Tag> tagList = tagMapper.query(tag);
            PageResult result = new PageResult();
            result.setRows(tagList);
            return result;
        });
    }

    @Override
    public RestResult<Long> editTag(Tag param) {
        return executeQuery(() -> {
            PromotionEntity entity = new PromotionEntity();
            entity.setLabelId(Long.valueOf(param.getId()));
            List<PromotionEntity> proList = promotionEntityMapper.findAll(entity);
            if (proList.size() != 0) {
                log.error("分类使用异常，labelId={}");
                throw new MarketException(MarketErrorCodeEnum.TYPE_ERROR, "已有表关联此标签，无法编辑");
            } else {
                Tag tag = new Tag();
                BeanUtils.copyProperties(param, tag);

                try {
                    tag.setUpdateTime(new Date());
                    tag.setUpdatorName(param.getUpdatorName());
                    tagMapper.update(tag);
                } catch (Exception e) {
                    log.error("名称保存异常，tagName={}");
                    throw new MarketException(MarketErrorCodeEnum.TAGNAME_ERROR, "此标签名已被使用，请重新命名");
                }
            }
            return null;
        });
    }

    @Override
    public RestResult<Long> delTag(Tag tag) {
        return executeQuery(() -> {
            PromotionEntity entity = new PromotionEntity();
            entity.setLabelId(Long.valueOf(tag.getId()));
            List<PromotionEntity> proList = promotionEntityMapper.findAll(entity);
            if (proList.size() != 0) {
                log.error("分类使用异常，labelId={}");
                throw new MarketException(MarketErrorCodeEnum.TYPE_ERROR, "已有表关联此标签，无法删除");
            } else {
                Long tagId = Long.valueOf(tag.getId());
                long flag = tagMapper.delete(tagId);
                return flag;
            }

        });
    }


}